package src.main.java.cn.edu.sdjzu.xg.eagle.dao.mapper;

import cn.edu.sdjzu.xg.eagle.dao.StudentDao;
import cn.edu.sdjzu.xg.eagle.entity.Student;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//20230214030-王桉锦
public  class StudentDaolmpl implements StudentDao {
    public boolean addStudent(Connection connection, Student student)
            throws SQLException, BaseException, InvocationTargetException {
        String sql_select = "INSERT INTO bysj22.student (id, no, name, user_id) " +
                "VALUES (?, ?, ?, ?)";
        PreparedStatement add_preparedStatement =
                connection.prepareStatement(sql_select);

        add_preparedStatement.setLong(1, student.getId());
        add_preparedStatement.setString(2, student.getNo());
        add_preparedStatement.setString(3, student.getName());
        User user = student.getUser();
        if(user != null) {
            add_preparedStatement.setInt(4, student.getUser().getId());
        }
        int affectedRowNum = add_preparedStatement.executeUpdate();
        return affectedRowNum > 0;
    }
    //202302104030-王桉锦
    public Student find(Connection connection, Long id) throws SQLException, BaseException, InvocationTargetException {
        String sql_select = "SELECT * from bysj22.student WHERE id = ? ";
        PreparedStatement pstmt_select = connection.prepareStatement(sql_select);
        pstmt_select.setLong(1, id);
        ResultSet resultSet_select = pstmt_select.executeQuery();
        Student desiredStudent = null;
        if(resultSet_select.next()) {
            String name = resultSet_select.getString("name");
            String no = resultSet_select.getString("no");
            desiredStudent = new Student(id, no, name);
        }
        JdbcHelper.close(resultSet_select, pstmt_select);
        return desiredStudent;
    }

    @Override
    public boolean update(Connection connection, Student student) throws SQLException, BaseException, InvocationTargetException {
        String sql_update =
                "UPDATE student SET no = ?, name = ? " +
                        "WHERE id = ?";
        PreparedStatement pstmt_update =
                connection.prepareStatement(sql_update);

        pstmt_update.setString(1, student.getName());
        pstmt_update.setString(2, student.getNo());
        pstmt_update.setLong(3, student.getId());


        int rowAffected = pstmt_update.executeUpdate();

        JdbcHelper.close(pstmt_update);
        return rowAffected > 0;
    }

    @Override
    public boolean delete(Connection connection, Integer id) throws SQLException, BaseException, InvocationTargetException {
            String sql_delete_id = "DELETE FROM student WHERE id = ?";
            PreparedStatement pstmt_delete =
                    connection.prepareStatement(sql_delete_id);
            pstmt_delete.setInt(1, id);
            int rowAffected = pstmt_delete.executeUpdate();
            JdbcHelper.close(pstmt_delete);
            return rowAffected > 0;
    }
}

